<template>
  <div class="upload-box">
    <!-- file-list 用于数据回显   limit  设置最大上传个数-->
    <el-upload
      action="#"
      list-type="picture-card"
      :http-request="updateJob"
      :file-list="fileList"
      :on-remove="removeFn"
      :limit="1"
      :class="{displayle:fileList.length>0}"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="precent>0 && precent <100" type="circle" :percentage="precent" />
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDQffQwCDzB8sMbdQxBD1d1B7pPlvsb6NQ', // 身份识别 ID
  SecretKey: 'sWw88uUqxqWzFH6g2A4kCPPDoBKH2Zrw' // 身份密钥
})

export default {
  data() {
    return {
      fileList: [],
      precent: 0
    }
  },
  methods: {
  // 删除
    removeFn() {
      this.fileList = []
    },
    // 图片上传
    updateJob(data) {
      this.fileList = [{ url: '' }]
      cos.putObject({
        Bucket: 'hr-80-1311280024', /* 必须 储存桶名字 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: Date.now().toString(), /* 必须 上传到腾讯云上的文件名 字符穿 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: (progressData) => {
          // 设置进度条
          this.precent = progressData.precent * 100
        }
      }, (err, data) => {
        console.log(err || data)
        if (!err) {
          // this.fileList.push({
          //   url: 'https://' + data.Location
          // })
          this.fileList[0].url = 'https://' + data.Location
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-box{
  position: relative;
  display: inline-block;
   .displayle{
  ::v-deep .el-upload--picture-card{
        display: none;
  }

}
.el-progress{
  position: absolute;
  top: 50%;
  left: 50%;
  transform:translate(-50%,-50%)
}
}

</style>

